/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admintool.imp03_data;

import admintool.AdminToolView;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class IMP03_City {

    private class City {

        private String plz = "";
        private String longitude = "";
        private String latitude = "";
        private String cityName = "";
        
        public City(String plz, String longitude, String latitude, String cityName) {
            this.plz = plz;
            this.longitude = longitude;
            this.latitude = latitude;
            this.cityName = cityName;
        }

        public String getCityName() {
            return cityName;
        }

        public String getLatitude() {
            return latitude;
        }

        public String getLongitude() {
            return longitude;
        }

        public String getPlz() {
            return plz;
        }
    }
    
    private static ArrayList<IMP03_City> cities = new ArrayList<IMP03_City>();

    /**
     * 
     * @return
     */
    public static IMP03_City getEmptyCityObject(){
        return new IMP03_City(AdminToolView.EMPTY, AdminToolView.EMPTY, AdminToolView.EMPTY, AdminToolView.EMPTY);
    }
    
    /**
     * 
     * @param plz
     * @return
     */
    public static IMP03_City getSpecificCitiesFromDB(String plz) {
        PreparedStatement ps = null;
        ResultSet rs = null;
        IMP03_City city = null;
        
        try {
            ps = AdminToolView.getDBConnection().prepareStatement("SELECT * FROM city WHERE plz = ?");
            ps.setString(1, plz);
            rs = ps.executeQuery();
            while(rs.next()){
                city = new IMP03_City(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null){
                    rs.close();
                }
                if(ps != null){
                    ps.close();
                }
            } catch (Exception e) {
            }
        }
        return city;
    }

    /**
     * 
     * @param reload
     * @return
     */
    public static ArrayList<IMP03_City> getAllAvailableCitiesFromDB(boolean reload){
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            if(reload == true || cities.isEmpty()){
                cities.clear();
                ps = AdminToolView.getDBConnection().prepareStatement("SELECT * FROM city");
                rs = ps.executeQuery();
                while(rs.next()){
                    cities.add(new IMP03_City(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null){
                    rs.close();
                }
                if(ps != null){
                    ps.close();
                }
            } catch (Exception e) {
            }
        }
        return cities;
    }
    
    private City city = null;
    
    /**
     * 
     * @return
     */
    public String getPLZ(){
        return city.getPlz();
    }
    
    /**
     * 
     * @return
     */
    public String getCityName(){
        return city.getCityName();
    }
    
    /**
     * 
     * @param plz
     * @param longitude
     * @param latitude
     * @param cityName
     */
    public IMP03_City(String plz, String longitude, String latitude, String cityName) {
        this.city = new City(plz, longitude, latitude, cityName);
    }
    
    @Override
    public String toString() {
        return city.getPlz();
    }
}
